# coding = utf-8

import Eblob.setting

from hbase2 import THBaseService
from hbase2.ttypes import TGet, TPut, TColumnValue
from thrift.protocol import TBinaryProtocol
from thrift.transport import TSocket


class Thrift2Connection(THBaseService.Client):

    def __init__(self):
        self.transport = TSocket.TSocket(Eblob.setting.host, Eblob.setting.port)
        self.protocol = TBinaryProtocol.TBinaryProtocol(self.transport)
        THBaseService.Client.__init__(self, iprot=self.protocol, oprot=None)
        self.transport.open()

    def get_tables(self):
        pass

    def _close_connection(self):
        self.transport.close()

    def _is_existed(self, table_name, row_key, is_like=False):
        if Eblob.setting.real_delete:
            if is_like:
                pass
            else:
                get = TGet()
                get.row = row_key
                return self.exists(table_name, get)
        else:
            pass

    def _save(self, table_name, row_key, maps):
        column_values = list()
        for key, value in maps:
            column_values.append(TColumnValue(key.split(':')[0], key.split(':')[1], value))
        column_values.append(TColumnValue('status:status', '1'))
        put = TPut(row_key, column_values)
        self.put(table_name, put)

    def _find(self, table_name, row_key_like, start_row, counts):
        if Eblob.setting.real_delete:
            pass
        else:
            pass

    def _get_one(self, table_name, row_key):
        if Eblob.setting.real_delete:
            pass
        else:
            get = TGet()
            get.row = row_key
            return self.get(table_name, get)

    def _delete(self, table_name, row_key):
        if Eblob.setting.real_delete:
            pass
        else:
            pass

    def real_is_existed(self, table_name, row_key, is_like=False):
        return self._is_existed(table_name, row_key, is_like)

    def real_save(self, table_name, row_key, maps):
        self._save(table_name, row_key, maps)

    def real_find(self, table_name, row_key_like, start_row, counts):
        return self._find(table_name, row_key_like, start_row, counts)

    def real_get(self, table_name, row_key):
        return self._get_one(table_name, row_key)

    def real_delete(self, table_name, row_key):
        self._delete(table_name, row_key)

    def real_delete_like(self, table_name, row_key_like):
        pass
